package com.example;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.AxdRiskUser;
import com.example.entity.axd.AxdAppUser;
import com.example.entity.axd.AxdFirm;
import com.example.entity.exceldto.ExcelDto;
import com.example.entity.exceldto.FirmExcelDto;
import com.example.mapper.AxdAppUserMapper;
import com.example.mapper.AxdFirmMapper;
import com.example.mapper.AxdRiskUserMapper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;
import java.security.SecureRandom;
import java.util.*;

import static java.lang.System.out;

/*
 *  @author 雨安
 *  类名： FirmCodeExcelApplication
 *  创建时间：2024/4/17
 */
@SpringBootTest
public class FirmCodeExcelApplication extends AnalysisEventListener<FirmExcelDto> {

    @Resource
    private AxdAppUserMapper axdAppUserMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String DIGITS = "0123456789";
    private static final String OPERATORS = "+-*/";
    private static final String ALL_CHARS = LOWERCASE + UPPERCASE + DIGITS + OPERATORS;


    public static String generateRandomPassword(int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder password = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            password.append(ALL_CHARS.charAt(random.nextInt(ALL_CHARS.length())));
        }
        return password.toString();
    }

    @Resource
    private AxdRiskUserMapper axdRiskUserMapper;
    @Test
    public void ha2(){
        List<AxdRiskUser> axdRiskUserList = axdRiskUserMapper.selectList(null);
        Integer count = 0;
        //遍历集合数据
        for (AxdRiskUser riskUser : axdRiskUserList) {
            if (Objects.nonNull(riskUser)){
                count++;
                out.println("测试" + count);
                String str = generateRandomPassword(10);
                //设置加密密码
                String encode = passwordEncoder.encode(str);
                //设置用户密码
                riskUser.setPassword(encode);
                //设置用户可见密码
                riskUser.setPasswordAdmin(str);
                //执行修改操作
                axdRiskUserMapper.updateById(riskUser);
            }//判断遍历对象不为 null

        }//for each

    }


    @Resource
    private AxdFirmMapper axdFirmMapper;
    @Test
    void wirte2(){
        String fileName = "D:\\Spring框架内容学习所有\\SpringSecurityDemo4\\src\\main\\resources\\PC端重置密码.xlsx";

        //表头字体颜色map 1为user中索引位置
        Map<Integer,Short> colorMap=new HashMap<>();

        colorMap.put(1, IndexedColors.BLUE.index);

        List<AxdRiskUser> axdRiskUserList = axdRiskUserMapper.selectList(null);

        for (AxdRiskUser riskUser : axdRiskUserList) {
            if (Objects.nonNull(riskUser) && Objects.nonNull(riskUser.getEduFirmId())){
                //设置企业名称
                AxdFirm axdFirm = axdFirmMapper.selectById(riskUser.getEduFirmId());
                //如果企业名称不为 null
                if (Objects.nonNull(axdFirm) && StringUtils.isNotBlank(axdFirm.getName())){
                    riskUser.setFirmName(axdFirm.getName());
                }
            }
        }//for each结束

        EasyExcel.write(fileName, AxdRiskUser.class).sheet("PC端用户密码").doWrite(axdRiskUserList);
    }

    @Test
    void wirte3(){
        String fileName = "D:\\Spring框架内容学习所有\\SpringSecurityDemo4\\src\\main\\resources\\小程序重置密码.xlsx";

        //表头字体颜色map 1为user中索引位置
        Map<Integer,Short> colorMap=new HashMap<>();

        colorMap.put(1, IndexedColors.BLUE.index);

        List<AxdAppUser> axdAppUserList = axdAppUserMapper.selectList(null);

        for (AxdAppUser appUser : axdAppUserList) {
            if (Objects.nonNull(appUser) && Objects.nonNull(appUser.getFirmId())){
                out.println(appUser + "我遍历的数据");
                //设置企业名称
                AxdFirm axdFirm = axdFirmMapper.selectById(appUser.getFirmId());
                //如果企业名称不为 null
                if (Objects.nonNull(axdFirm) && StringUtils.isNotBlank(axdFirm.getName())){
                    appUser.setFirmName(axdFirm.getName());
                }
            }
        }//for each结束

        EasyExcel.write(fileName, AxdRiskUser.class).sheet("小程序密码").doWrite(axdAppUserList);
    }


    @Test
    public void haha(){
        List<AxdAppUser> axdAppUserList = axdAppUserMapper.selectList(null);
        Integer count = 0;
        //遍历集合数据
        for (AxdAppUser axdAppUser : axdAppUserList) {
            if (Objects.nonNull(axdAppUser)){
                String str = generateRandomPassword(10);
                count++;

                //生成密码
                out.println(str);
                //设置加密密码
                String encode = passwordEncoder.encode(str);
                //设置用户密码
                axdAppUser.setPassword(encode);
                //生成密码
                out.println(str + "我是加密的密码" + encode);
                //设置用户可见密码
                axdAppUser.setAdminPassword(str);
                //执行修改操作
                axdAppUserMapper.updateById(axdAppUser);
            }//判断遍历对象不为 null

        }//for each

    }


    @Test
    void wirte(){
        String fileName = "D:\\Spring框架内容学习所有\\SpringSecurityDemo4\\src\\main\\resources\\公司信用代码.xlsx";

        //表头字体颜色map 1为user中索引位置
        Map<Integer,Short> colorMap=new HashMap<>();

        colorMap.put(1, IndexedColors.BLUE.index);

        List<FirmExcelDto> list = new ArrayList<>();

        EasyExcel.write(fileName, FirmExcelDto.class).sheet("信用代码").doWrite(list);
    }


    @Autowired


    //测试拿一次数据优先创建获取数据的集合
    public void test(){

    }


    @Test
    void aaa() {
        //设置读取的文件地址
        String fileName = "D:\\Spring框架内容学习所有\\SpringSecurityDemo4\\src\\main\\resources\\公司信用代码.xlsx";
        //渎职 1.文件名 2.转换实体类 3.监听对象
        ExcelReader reader = EasyExcel.read(fileName, FirmExcelDto.class, new FirmCodeExcelApplication())
                //构建
                .build();
        // 读取Excel文件中的数据
        reader.read();
        // 关闭reader
        reader.finish();





    }

    //创建excel初始化读取集合
    private static List<FirmExcelDto> firmExcelDtoList = new LinkedList<>();

    @Override
    public void invoke(FirmExcelDto firmExcelDto, AnalysisContext analysisContext) {
        firmExcelDtoList.add(firmExcelDto);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        out.println("数据读取完毕了。。。。");
    }



}
